<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-6-8
 * Time: 16:29
 */

namespace app\web\controller;
use app\common\controller\Api;
use app\common\model\Factor;
use app\common\model\Fenorder;
use app\web\controller\Express;
use PhpOffice\PhpSpreadsheet\Calculation\LookupRef\ExcelMatch;
use think\Db;

/**
 * 头牌控制器
 * @package app\index\controller
 */
class TopCard extends Auth
{
    public $noNeedLogin = [];

    /**
     *获取头牌列表
     * @return array
     */
    public function lists()
    {
        if ($this->request->isPost()) {
            $userInfo = $this->userInfo;
            $page = $this->request->post('page');
            $limit = $this->request->post('limit');
            $data = Db::name('qt_top_card')
                ->field('t.id,t.rule,t.engineer_id,t.status,t.end_time,t.content,t.create_time,u.real_name')
                ->alias('t')
                ->leftJoin('qt_users u','u.id = t.user_id')
                ->where(['t.store_id' => $userInfo['store_id']])
                ->where('t.end_time','>',time())
                ->order('t.id desc')
                ->paginate($limit)->each(function ($item){
                    $engineerIds = explode(',',$item['engineer_id']);
                    $engineer_name = Db::name('qt_users')->whereIn('id',$engineerIds)->column('work_number');
                    $item['engineer_name'] = implode(',',$engineer_name);
                    return $item;
                });
            $this->result('查询成功', $data, 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     *搜索头牌列表
     * @return array
     */
    public function search()
    {
        if ($this->request->isPost()) {
            $userInfo = $this->userInfo;
            $getData = $this->request->post();
            $page = $this->request->post('page');
            $limit = $this->request->post('limit');
            $sql = Db::name('qt_top_card')
                ->alias('t')
                ->leftJoin('qt_users u','u.id = t.user_id')
                ->field('t.id,t.rule,t.engineer_id,t.status,t.end_time,t.content,t.create_time,u.real_name')
                ->where(['t.store_id' => $userInfo['store_id']]);
            if(isset($getData['start_time']) && !empty($getData['start_time'])){
                $startTime = strtotime($getData['start_time']);
                $endTime = strtotime($getData['end_time']);
                $sql->whereBetweenTime('t.create_time',$startTime,$endTime);
            }
            if(isset($getData['work_number']) && !empty($getData['work_number'])){
                $sql->where('u.work_number',$getData['work_number']);
            }
            $data = $sql->order('t.id desc')
                ->paginate($limit)->each(function ($item){
                    $engineerIds = explode(',',$item['engineer_id']);
                    $engineer_name = Db::name('qt_users')->whereIn('id',$engineerIds)->column('work_number');
                    $item['engineer_name'] = implode(',',$engineer_name);
                    return $item;
                });
            $this->result('查询成功', $data, 200);
        }
        $this->result('无效的请求', '', 204);
    }

    /**
     * 添加头牌
     */
    public function add(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['rule']) || empty($getData['status']) || empty($getData['engineer_id']) || empty($getData['end_time'])){
                $this->result('缺少参数', '', 204);
            }
            $insert['user_id'] = $this->userInfo['id'];
            $insert['store_id'] = $this->userInfo['store_id'];
            $insert['engineer_id'] = $getData['engineer_id'];
            $insert['end_time'] =strtotime($getData['end_time']);
            $insert['create_time'] = time();
            $insert['content'] = $getData['content'];
            $insert['rule'] = $getData['rule'];
            $save = Db::name('qt_top_card')->insert($insert);
            if($save){
                $this->result('添加成功', '', 200);
            }
            $this->result('添加失败', '', 204);
        }
        $this->result('无效的请求', '', 204);
    }

    /**
     * 编辑保存头牌
     */
    public function save(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['rule']) || empty($getData['status']) || empty($getData['engineer_id']) || empty($getData['end_time']) || empty($getData['id'])){
                $this->result('缺少参数', '', 204);
            }
            $getData['end_time'] =strtotime($getData['end_time']);
            $update= Db::name('qt_top_card')->where('id',$getData['id'])->update($getData);
            if($update !== false){
                $this->result('修改成功', '', 200);
            }
            $this->result('修改失败', '', 204);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 编辑保存头牌
     */
    public function edit(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['id'])){
                $this->result('缺少参数', '', 204);
            }
            $data= Db::name('qt_top_card')->where('id',$getData['id'])->find();
            if(!empty($data)){
                $engineerIds = explode(',',$data['engineer_id']);
                $engineer_name = Db::name('qt_users')->whereIn('id',$engineerIds)->column('work_number');
                $data['engineer_name'] = implode(',',$engineer_name);
            }
            $this->result('保存成功', $data, 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 删除头牌
     */
    public function delete(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['id'])){
                $this->result('缺少参数', '', 204);
            }
            $data= Db::name('qt_top_card')->where('id',$getData['id'])->delete();
            $this->result('删除成功', $data, 200);
        }
        $this->result('无效的请求', '', 204);
    }



}
